package ciyuan.Entity.FlameBall;

import ciyuan.Damage.MyDamageSource;
import ciyuan.Entity.EntityRegisterManager;
import ciyuan.Player.Skill.Passive.MagicIncrease;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityPredicate;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.DamagingProjectileEntity;
import net.minecraft.network.IPacket;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.fml.network.NetworkHooks;

import java.util.List;

public class FlameBallEntity extends DamagingProjectileEntity {

    public FlameBallEntity(LivingEntity entity, double powerX, double powerY, double powerZ, World world) {
        super(EntityRegisterManager.flameball.get(), entity,powerX,powerY,powerZ,world);
        this.setNoGravity(true);
    }

    public FlameBallEntity(EntityType<FlameBallEntity> entityEntityType, World world) {
        super(entityEntityType,world);
    }

    @Override
    public boolean isPickable() {
        return false;
    }

    @Override
    protected boolean shouldBurn() {
        return false;
    }

    @Override
    public IPacket<?> getAddEntityPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }

    private static final EntityPredicate PARTNER_TARGETING = (new EntityPredicate()).range(2.0D);

    @Override
    public void tick() {
        super.tick();
        if (this.level.isClientSide) {
            return;
        }
        if (this.getOwner()!=null) {
            if (this.distanceTo(this.getOwner())>20 && this.removed==false ) {
                SkillUtils.removeEntity(this);
            }
        }

    }

    @Override
    protected void onHitBlock(BlockRayTraceResult p_230299_1_) {
        SkillUtils.removeEntity(this);
    }

    @Override
    protected void onHitEntity(EntityRayTraceResult entityRayTraceResult) {
        if (this.level.isClientSide) {
            return;
        }
        if (!entityRayTraceResult.getEntity().isInvulnerable() && entityRayTraceResult.getEntity() instanceof LivingEntity) {
            this.level.playSound(null,this.getX(),this.getY(),this.getZ(), SoundEvents.GENERIC_EXPLODE, SoundCategory.HOSTILE,1.0F,1.0F);
            ((ServerWorld)this.level).sendParticles(ParticleTypes.FLAME, this.getX() , this.getY()+1, this.getZ(), 5, 0.1, 0.1D, 0.1, 0.1D);
            entityRayTraceResult.getEntity().hurt(DamageSource.mobAttack((LivingEntity) getOwner()),10);
            MagicIncrease.onSkillHit(getOwner());

            List<LivingEntity> livingEntityList = this.level.getNearbyEntities(LivingEntity.class,PARTNER_TARGETING,(LivingEntity) entityRayTraceResult.getEntity(),
                    new AxisAlignedBB(this.getX()+1,this.getY()+1,this.getZ()+1,this.getX()-1,this.getY()-1,this.getZ()-1));
            for (LivingEntity livingEntity : livingEntityList) {
                livingEntity.hurt(MyDamageSource.sputter((PlayerEntity) getOwner()),5);
                MagicIncrease.onSkillHit(getOwner());
            }
        }
        SkillUtils.removeEntity(this);
    }

    @Override
    public boolean hurt(DamageSource p_70097_1_, float p_70097_2_) {
        return false;
    }

    @Override
    protected boolean canHitEntity(Entity p_230298_1_) {
        return true;
    }
}
